//
//  UITableView_refresh.swift
//  MyLibrary
//
//  Created by 周博 on 2020/6/18.
//  Copyright © 2020 ncit. All rights reserved.
//

import UIKit
import MJRefresh


public extension UITableView {

    /// 下拉刷新
    func setHeaderRefreshLoadNewData(_ block: @escaping () -> Void) {
        mj_header = MJRefreshNormalHeader(refreshingBlock: {
            block()
        })
        /// 导航栏下隐藏MJ_header
        mj_header!.isAutomaticallyChangeAlpha = true
    }

    /// 上拉加载更多
    func setFooterRefreshLoadMoreData(_ block: @escaping () -> Void) {
        mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {
            block()
        })
    }

    /// 设置刷新header footer
    func setRefreshHeaderTitle(_ title: String?) {
        (mj_header as? MJRefreshStateHeader)?.setTitle(title ?? "", for: .idle)
    }

    func setRefreshHeaderBgColor(_ color: UIColor?) {
        (mj_header as? MJRefreshStateHeader)?.backgroundColor = color
    }

    func setRefreshHeaderColor(_ color: UIColor?) {
        if let color = color {
            (mj_header as? MJRefreshStateHeader)?.stateLabel!.textColor = color
        }
        if let color = color {
            (mj_header as? MJRefreshStateHeader)?.lastUpdatedTimeLabel!.textColor = color
        }
    }

    func setRefreshFooterTitle(_ title: String?) {
        (mj_footer as? MJRefreshBackNormalFooter)?.setTitle(title ?? "", for: .idle)
    }

    func setRefreshFooterBgColor(_ color: UIColor?) {
        (mj_footer as? MJRefreshBackNormalFooter)?.backgroundColor = color
    }

    func setRefreshFooterColor(_ color: UIColor?) {
        if let color = color {
            (mj_footer as? MJRefreshBackNormalFooter)?.stateLabel!.textColor = color
        }
    }



    /// 开始 结束 刷新状态
    func endRefresh() {
        mj_header?.endRefreshing()
        mj_footer?.endRefreshing()
    }

    func beginRefreshing() {
        mj_header?.beginRefreshing()
    }

    func endRefreshingWithNoMoreData() {
        mj_footer?.endRefreshingWithNoMoreData()
    }

    func resetNoMoreData() {
        mj_footer?.resetNoMoreData()
    }

}
